#!/usr/bin/perl
BEGIN {
    push @INC, '/home/xuqy/dmoz/modules';
}
#use warnings;
use strict;
use File::Spec;
use YAML;
use FocusCrawler::BestFirst;
use FocusCrawler::Accelerated;
use FocusCrawler::FirstOrder;

my ($base_dir, $working_dir, $method, $seeds_file, $min_accuracy) = @ARGV;

my $config_file = 'config';

my $params_ref = YAML::LoadFile($config_file);

$base_dir = File::Spec->rel2abs($base_dir) if defined $base_dir;

$params_ref->{base_dir} = $base_dir if defined $base_dir;
$params_ref->{working_dir} = $working_dir if defined $working_dir;
$params_ref->{method} = $method if defined $method;
$params_ref->{seeds_file} = $seeds_file if defined $seeds_file;
$params_ref->{min_accuracy} = $min_accuracy if defined $min_accuracy;

$method = $params_ref->{method};

if (not $params_ref->{resume_flag} and exists $params_ref->{learn_dir}) {
    my $src_dir = $params_ref->{base_dir}.'/'.$params_ref->{learn_dir};
    my $dst_dir = $params_ref->{base_dir}.'/'.$params_ref->{working_dir};
    mkdir $dst_dir unless -d $dst_dir;
    for (qw(metas_db_file url_db_file dump_db_file digest_db_file)) {
        my $file = $params_ref->{$_};
        my $dst_file = "$dst_dir/$file";
        unlink $dst_file if -e $dst_file;
        symlink("$src_dir/$file", $dst_file) or die "$!";
    }
}

if ($method eq 'BestFirst') {
    FocusCrawler::BestFirst->init($params_ref);
    FocusCrawler::BestFirst->run();
}
elsif ($method eq 'Accelerated') {
    FocusCrawler::Accelerated->init($params_ref);
    FocusCrawler::Accelerated->run();
}
elsif ($method eq 'FirstOrder') {
    FocusCrawler::FirstOrder->init($params_ref);
    FocusCrawler::FirstOrder->run();
}
else {
    die "Unknown focused crawling method: $method!";
}
